//
// Created by enemy1205 on 2021/7/23.
//
#include "Process_manage.h"

ProcessManager::ProcessManager() {
//初始化属性
    this->Init_competition();
    this->createPlayer();
    this->loadRecord();
}

void ProcessManager::createPlayer() {
    string nameseed = "ABCDEFGHIJKL";
    for (int i = 0; i < nameseed.size(); ++i) {
        string name = "选手";
        name += nameseed[i];
        Player pl;
        pl._Name = name;
        for (int j = 0; j < 2; ++j) {
            pl._Score[j] = 0;
        }
        this->v1.emplace_back(i + 10086);
        this->m_Player.insert(make_pair(i + 10086, pl));
    }
}

ProcessManager::~ProcessManager() {

}

void ProcessManager::show_menu() {
    {
        cout << "********************************************" << endl;
        cout << "**************  欢迎参加比赛 *****************" << endl;
        cout << "*************  1.开始演讲比赛  *************" << endl;
        cout << "*************  2.查看往届记录  *************" << endl;
        cout << "*************  3.清空比赛记录  *************" << endl;
        cout << "*************  0.退出比赛程序  *************" << endl;
        cout << "********************************************" << endl;
        cout << endl;
    }
}

void ProcessManager::Init_competition() {
    //容器保证为空
    this->v1.clear();
    this->v2.clear();
    this->vVictory.clear();
    this->m_Player.clear();
    //初始化比赛轮数
    this->m_Index = 1;
    this->m_Record.clear();
}

void ProcessManager::startCompetition() {
    /*第一轮比赛*/
    /*抽签*/
    this->CompetitionDraw();
    /*比赛*/
    this->Competition();
    /*显示晋级结果*/
    this->ShowScore();
    /*第二轮比赛*/
    this->m_Index++;
    /*抽签*/
    this->CompetitionDraw();
    /*比赛*/
    this->Competition();
    /*显示晋级结果*/
    this->ShowScore();
    /*保存分数*/
    this->saveRecord();
    cout << "本届比赛完毕!" << endl;
    getchar();
    show_menu();

}

void ProcessManager::saveRecord() {
    ofstream ofs;
    ofs.open("Competetion.csv", ios::out | ios::app);/*用输出的方式打开文件  -- 写文件*/
    for (auto it = vVictory.begin(); it != vVictory.end(); it++) {
        ofs << *it << "," << m_Player[*it]._Score[1] << ",";
    }
    ofs << endl;
    ofs.close();
    cout << "记录已经保存" << endl;
}

void ProcessManager::CompetitionDraw() {
    cout << "第 << " << this->m_Index << " >> 轮比赛选手正在抽签" << endl;
    cout << "---------------------" << endl;
    cout << "抽签后比赛顺序如下：" << endl;
    if (this->m_Index == 1) {
        random_shuffle(v1.begin(), v1.end());
        for (auto it = v1.begin(); it != v1.end(); it++) {
            cout << *it << "  ";
        }
        cout << endl;
    } else {
        random_shuffle(v2.begin(), v2.end());
        for (auto it = v2.begin(); it != v2.end(); it++) {
            cout << *it << "  ";
        }
        cout << endl;
    }
    cout << "---------------------" << endl;
    cout << "按下Enter键继续" << endl;
    getchar();
    getchar();
    cout << endl;
}

void ProcessManager::Competition() {
    cout << "------------- 第" << this->m_Index << "轮正式比赛开始：------------- " << endl;
    map<double, int, greater<int>> groupScore;
    int num = 0; //记录人员数，6个为1组
    vector<int> v_Src;   //比赛的人员容器
    if (this->m_Index == 1) {
        v_Src = v1;
    } else {
        v_Src = v2;
    }
    for (auto it = v_Src.begin(); it != v_Src.end(); it++) {
        num++;
        deque<double> Scores;
        /*10位评委打分*/
        for (int i = 0; i < 10; ++i) {
            double score = (rand() % 401 + 600) / 10.f; /*60~100*/
            Scores.emplace_back(score);
        }
        sort(Scores.begin(), Scores.end(), greater<double>());/*降序*/
        Scores.pop_front();
        Scores.pop_back();
        double sum = accumulate(Scores.begin(), Scores.end(), 0.0f);
        double avg = sum / double(Scores.size());
        this->m_Player[*it]._Score[this->m_Index - 1] = avg;
        groupScore.insert(make_pair(avg, *it));
        if (num % 6 == 0) {
            cout << "第" << num / 6 << "小组比赛名次：" << endl;
            for (auto it1 = groupScore.begin(); it1 != groupScore.end(); it1++) {
                cout << "编号: " << it1->second << " 姓名： " << this->m_Player[it1->second]._Name << " 成绩： "
                     << this->m_Player[it1->second]._Score[this->m_Index - 1] << endl;
            }
            int count = 0;
            /*取前三名*/
            for (auto it2 = groupScore.begin(); it2 != groupScore.end() && count < 3; it2++, count++) {
                if (this->m_Index == 1) v2.emplace_back((*it2).second);
                else vVictory.emplace_back((*it2).second);
            }
            groupScore.clear();
            cout << endl;
        }
    }
    cout << "------------- 第" << this->m_Index << "轮比赛完毕  ------------- " << endl;
    getchar();
}

void ProcessManager::ShowScore() {
    cout << "---------第" << this->m_Index << "轮晋级选手信息如下：-----------" << endl;
    vector<int> v;
    if (this->m_Index == 1) {
        v = v2;
    } else {
        v = vVictory;
    }
    for (auto it = v.begin(); it != v.end(); it++) {
        cout << "选手编号：" << *it << " 姓名： " << m_Player[*it]._Name << " 得分： " << m_Player[*it]._Score[this->m_Index - 1]
             << endl;
    }
    cout << endl;
    getchar();
//    this->show_menu();
}

void ProcessManager::loadRecord() {
    ifstream ifs("speech.csv", ios::in); //输入流对象 读取文件

    if (!ifs.is_open()) {
        this->fileIsEmpty = true;
        cout << "文件不存在！" << endl;
        ifs.close();
        return;
    }

    char ch;
    ifs >> ch;
    if (ifs.eof()) {
        cout << "文件为空!" << endl;
        this->fileIsEmpty = true;
        ifs.close();
        return;
    }

    //文件不为空
    this->fileIsEmpty = false;

    ifs.putback(ch); //读取的单个字符放回去

    string data;
    int index = 0;
    while (ifs >> data) {
        //cout << data << endl;
        vector<string> v;

        int pos = -1;
        int start = 0;

        while (true) {
            pos = data.find(",", start); //从0开始查找 ','
            if (pos == -1) {
                break; //找不到break返回
            }
            string tmp = data.substr(start, pos - start); //找到了,进行分割 参数1 起始位置，参数2 截取长度
            v.push_back(tmp);
            start = pos + 1;
        }

        this->m_Record.insert(make_pair(index, v));
        index++;
    }

    ifs.close();
}

void ProcessManager::showRecord() {
    for (int i = 0; i < this->m_Record.size(); i++) {
        cout << "第" << i + 1 << "届 " <<"冠军编号：" << this->m_Record[i][0] << " 得分：" << this->m_Record[i][1] << "  亚军编号：" << this->m_Record[i][2]
             << " 得分：" << this->m_Record[i][3] << "  季军编号：" << this->m_Record[i][4] << " 得分：" << this->m_Record[i][5]
             << endl;
    }
    getchar();
}
void ProcessManager::clearRecord() {
    cout << "确认清空？" << endl;
    cout << "1、确认" << endl;
    cout << "2、返回" << endl;

    int select = 0;
    cin >> select;

    if (select == 1)
    {
        //打开模式 ios::trunc 如果存在删除文件并重新创建
        ofstream ofs("speech.csv", ios::trunc);
        ofs.close();

        //初始化属性
        this->Init_competition();

        //创建选手
        this->createPlayer();

        //获取往届记录
        this->loadRecord();


        cout << "清空成功！" << endl;
    }

    getchar();
}
void ProcessManager::exit_system() {
    cout << "欢迎下次使用" << endl;
    exit(0);
}